/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.gui.customer;

import com.kimstore.bean.Customer;
import com.kimstore.factory.DaoFactory;
import com.kimstore.main.controller.CustomerTabController;

/**
 *
 * @author Administrator
 */
public class NewCustomerController {

    private CustomerTabController parentController;
    private NewCustomerWindow window;

    public NewCustomerController(CustomerTabController parentController) {
        this.parentController = parentController;
    }

    public void displayWindow() {
        window = new NewCustomerWindow(this);
        window.showWindow();
    }

    public void fromWindow(Customer c) {
        DaoFactory.getInstance().getCustomerDao().saveOrUpdate(c);
        
        parentController.createNewCustomerControllerCallback(c);
    }

    public void validate(Customer customer) throws Error {
        if(customer.getName() == null || customer.getName().isEmpty()) {
            throw new Error("<html><b>Customer Name</b> has not been filled up.</html>");
        }
        
        boolean exists = DaoFactory.getInstance().getCustomerDao().existsName(customer.getName());
        
        if(exists) {
            throw new Error("<html><b>Customer Name</b> has already been taken.</html>");
        }
    }
    
    
}
